package com.cnswhy.cloud.server.app.controller.merchant;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnswhy.cloud.common.core.exception.BizException;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.app.controller.BaseController;
import com.cnswhy.cloud.server.core.dto.MerchantSaveDTO;
import com.cnswhy.cloud.server.core.dto.MerchantUpdateDTO;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.IotTerminalInfo;
import com.cnswhy.cloud.server.core.entity.Merchant;
import com.cnswhy.cloud.server.core.service.IotChargingLineInfoService;
import com.cnswhy.cloud.server.core.service.IotChargingLineScaleService;
import com.cnswhy.cloud.server.core.service.IotTerminalInfoService;
import com.cnswhy.cloud.server.core.service.merchant.MerchantService;
import com.cnswhy.cloud.server.core.vo.RespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "商家")
@Controller
@RequestMapping("/merchant")
@Slf4j
public class CustomerMerchantController extends BaseController {
    @Autowired
    IotChargingLineInfoService iotChargingLineInfoService;
    @Autowired
    IotChargingLineScaleService iotChargingLineScaleService;
    @Autowired
    MerchantService merchantService;
    @Autowired
    IotTerminalInfoService iotTerminalInfoService;
    @ApiOperation(value = "添加商家", httpMethod = "POST")
    @RequestMapping(value = "/save", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo save(HttpServletRequest request, MerchantSaveDTO merchantSaveDTO) {
        try{
            Customer customer = getLoginCustomer(request);
            Merchant merchant = BeanUtil.toBean(merchantSaveDTO, Merchant.class);
            merchant.setCustomerId(customer.getId());
            merchant.setIsPublic(0);
            merchant.setStatus(1);
            merchantService.save(merchant);
            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("添加商家失败");
        }
    }
    @ApiOperation(value = "更新商家信息", httpMethod = "POST")
    @RequestMapping(value = "/update", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody

    public RespVo update(HttpServletRequest request, MerchantUpdateDTO merchantUpdateDTO) {
        try{
            Merchant merchant = BeanUtil.toBean(merchantUpdateDTO, Merchant.class);
            merchantService.updateById(merchant);
            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("取消关联设备失败");
        }
    }
    @ApiOperation(value = "删除商家", httpMethod = "POST")
    @RequestMapping(value = "/remove", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo remove(HttpServletRequest request, Long id) {
        try{
            merchantService.removeById(id);

            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("取消关联设备失败");
        }
    }
    @ApiOperation(value = "成为公海商户", httpMethod = "POST")
    @RequestMapping(value = "/setPublic", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo setPublic(HttpServletRequest request, Long merchantId) {
        try{
            Merchant merchant = merchantService.getById(merchantId);
            merchant.setIsPublic(1);
            merchantService.updateById(merchant);

            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("取消关联设备失败");
        }
    }
    @ApiOperation(value = "取消公海商户", httpMethod = "POST")
    @RequestMapping(value = "/resetPublic", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo resetPublic(HttpServletRequest request, Long merchantId) {
        try{
            Merchant merchant = merchantService.getById(merchantId);
            merchant.setIsPublic(0);
            merchantService.updateById(merchant);

            return RespVo.success();
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("取消关联设备失败");
        }
    }
    @ApiOperation(value = "查询公海商户", httpMethod = "POST")
    @RequestMapping(value = "/queryPublicMerchant", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo queryPublicMerchant(HttpServletRequest request, @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        try{
            Page<Merchant> page = new Page<>(currentPage, pageSize);
            IPage<Merchant> pageList = merchantService.page(page, Wraps.<Merchant>lbQ().eq(Merchant::getIsPublic, 1).orderByDesc(Merchant::getId));

            return RespVo.success(pageList);
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("查询公海商户失败");
        }
    }
    @ApiOperation(value = "查看商家关联的设备", httpMethod = "POST")
    @RequestMapping(value = "/queryTerminal", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo queryTerminal(HttpServletRequest request,@RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, Long merchantId) {
        try{
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("iotTerminalInfo",iotTerminalInfoService.pageByMerchant(currentPage,pageSize,merchantId));
            return RespVo.success(jsonObject);
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            return RespVo.fail("取消关联设备失败");
        }
    }
    @ApiOperation(value = "查询所有商家", httpMethod = "POST")
    @RequestMapping(value = "/list", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public RespVo list(HttpServletRequest request, @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                  @RequestParam(name = "type") Integer addType) {
        try{
            Customer customer = getLoginCustomer(request);
            Page<Merchant> page = new Page<>(currentPage, pageSize);
            IPage<Merchant> pageList = null;
            if(addType == null){
                pageList = merchantService.page(page, Wraps.<Merchant>lbQ().eq(Merchant::getCustomerId, customer.getId()).orderByDesc(Merchant::getId));
            }else{
                pageList = merchantService.page(page, Wraps.<Merchant>lbQ().eq(Merchant::getCustomerId, customer.getId())
                        .eq(Merchant::getAddType,addType).orderByDesc(Merchant::getId));
            }

            return RespVo.success(pageList);
        } catch (BizException e) {
            return RespVo.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return RespVo.fail("查询设备失败");
        }
    }
}
